package com.nacl.webflux.controller;

import com.nacl.webflux.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @author Mr.NaCl
 * @since 2024/9/26
 */
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Resource
    private DemoService demoService;

    /**
     * Mono 单声道   Flux 和 Mono 是 Reactor 库中的核心概念，用于构建响应式数据流
     * @return
     */
    @GetMapping("/monoOne")
    public Mono<Object> monoOne() {
        // method one
        log.info("monoOne执行了一次");
        String data = demoService.getOneResult("monoTest()");
        return Mono.just(data);

    }

    @GetMapping("/monoTest")
    public Mono<Object> monoTest() {
        // method two
        return Mono.create(e -> {
            String data = demoService.getOneResult("monoTest()");
            e.success(data);
        });
    }


    @GetMapping("/fluxTest")
    public Flux<Object> fluxTest() {
        // method one
        //        List<String> list = getMultiResult("fluxTest()");
        //        return Flux.fromIterable(list);

        // method two
        return Flux.fromIterable(demoService.getMultiResult("fluxTest()"));
    }

}
